/**
 * Created by hxpeng on 2017/3/24.
 */
(function () {
    'use strict';

    angular.module('App.common')
        .directive('dynamicUploadPic', dynamicUploadPic);

    /**
     * 发表动态上传图片，添加图片div和描述div
     * @returns {{restrict: string, link: link}}
     */
    function dynamicUploadPic() {
        return {
            restrict:'E',
            replace :true,
            template:'<div class="img-box" ng-hide="myCheck"><img>' +
            '<button class="button button-icon ion-close-circled item-body-remove-btn"></button><br/><textarea style="width: 100%;padding: 5px;" placeholder="输入照片描述...." rows="2" ng-trim="false" maxlength="2000"></textarea></div>',
            link: function(scope, elem, attr) {
                scope.$watch('per',function(nowVal){
                    var img = nowVal;
                    var elem_child = elem[0].childNodes;
                    angular.forEach(elem_child,function(obj){
                        if(obj.nodeName == "IMG"){
                            obj.setAttribute('src',img.url);
                            obj.setAttribute('name',img.serverId);
                            obj.onclick=function(){
                                scope.previewImg(img.url);
                            }
                        }
                        if(obj.nodeName == "BUTTON"){
                            obj.onclick=function(){
                                scope.removePicture(img.url,img.imgIndex);
                            }
                        }

                    })
                    autosize(document.querySelectorAll('textarea'));
                })

            }
        };
    }

})();